﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Text;
using System.Windows.Forms;
using System.Runtime.Serialization.Formatters.Binary;
using System.IO;

namespace HAZ.WinKeyGen
{
    public partial class MainForm : Form
    {
        public MainForm()
        {
            InitializeComponent();
        }

        private void btnGerar_Click(object sender, EventArgs e)
        {
            try
            {
                //byte[] Output;
                SaveFileDialog savefile = new SaveFileDialog();
                savefile.DefaultExt = "license";
                savefile.FileName   = txtSerial.Text.Trim();
                savefile.Filter     = "Arquivos de Licença (*.license)|*.licence|Todos os arquivos (*.*)|*.*";

                DialogResult result = savefile.ShowDialog();

                if (result == DialogResult.OK)
                {
                    License license     = new License();
                    license.SerialKey   = txtSerial.Text.Trim();
                    license.Vendedor    = txtVendedor.Text.Trim();
                    license.DataCompra  = dtpDataCompra.Value;
                    license.DataVencto  = dtpDataVencto.Value;
                    license.Corporativo = cbCorporativo.Checked;
                    license.Obs         = txtObs.Text.Trim();
                    license.GenerateLicence();

                    BinaryFormatter bf = new BinaryFormatter();
                    
                    Stream s = File.Open(savefile.FileName, FileMode.Create);

                    //Output = Serialization.ObjectToBinary(license);

                    bf.Serialize(s, license);
                    s.Flush();
                    s.Close();

                    MessageBox.Show("Licença Gerada com Sucesso!");
                }
            }
            catch(Exception ex)
            {
                MessageBox.Show(ex.Message);
            }
        }

        private void btnGetSerial_Click(object sender, EventArgs e)
        {
            txtSerial.Text = Session.Serial;
        }
    }
}
